package cn.knightzz.controller;


import cn.knightzz.entity.JsonResult;
import cn.knightzz.entity.SysFile;
import cn.knightzz.service.SysFileService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author knightzz
 * @since 2021-05-01
 */
@Slf4j
@RestController
@RequestMapping("/file")
public class SysFileController {

    @Resource
    SysFileService fileService;

    @PostMapping("upload")
    public JsonResult<SysFile> upload(@RequestParam("file") MultipartFile mf, HttpServletRequest request) throws IOException {
        // 获取参数
        String fileName = request.getParameter("fileName");
        String uid = request.getParameter("uid");
        String userId = request.getParameter("userId");
        String fileType = request.getParameter("fileType");

        // 打印参数
        log.debug(fileName);
        log.debug(uid);
        log.debug(userId);
        log.debug(fileType);

        JsonResult<SysFile> result = new JsonResult<>("200" , "上传成功111!");
        SysFile sysFile = new SysFile();
        sysFile.setId(Long.parseLong(uid));
        sysFile.setFileName(fileName);
        sysFile.setAuthorId(Long.parseLong(userId));
        sysFile.setFileType(fileType);
        fileService.upload(sysFile, mf.getInputStream());
        result.setData(sysFile);
        return result;
    }

    @DeleteMapping("/{id}")
    public JsonResult<SysFile> deleteFile(@PathVariable Long id){
        fileService.deleteFile(id);
        JsonResult<SysFile> result = new JsonResult<>("200" , "删除成功!");
        return result;
    }
}

